package br.com.perkins.myproject.service.exception;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.stereotype.Service;

@Service
public class ServiceExceptionMessageService {
	private static Map<Integer, String> errorMessages;
	static {
		if (errorMessages == null) {
			InputStream inputStream = ServiceExceptionMessageService.class.getResourceAsStream("errorMessages.properties");
	        Properties properties = new Properties();
	        try {
	            properties.load(inputStream);
	            inputStream.close();
	        }
	        catch (IOException exception) {
	        	errorMessages = new HashMap<Integer, String>();
	        	errorMessages.put(999, "Error reading Error Message File");
	        }

	        errorMessages = new HashMap<Integer, String>();
	        for (Object propertie : properties.keySet()) {
	        	errorMessages.put(Integer.valueOf((String)propertie), (String) properties.get(propertie));
			}

	        properties = null;
		}
	}

	public String getServiceException (Integer exceptionNumber) {
		if (errorMessages.containsKey(999)) {
			return errorMessages.get(exceptionNumber);
		} else if (errorMessages.containsKey(exceptionNumber)) {
			return errorMessages.get(exceptionNumber);
		} else {
			return "Message not found";
		}

	}

}